<?php
    // +----------------------------------------------------------------------
    // | 图片文件管理
    // +----------------------------------------------------------------------
    // | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
    // +----------------------------------------------------------------------
    // | Author: 微尘 <yicmf@qq.com>
    // +----------------------------------------------------------------------
    namespace app\controller\file;

    use app\event\file\Image as ImageEvent;
    use think\facade\Db;

    class UEditor extends Common
    {

        /* 上传图片 */
        public function upload()
        {
            if ($this->request->file('imgFile')) {
                $file = $this->request->file('imgFile');
                $image = new ImageEvent();
                $data = $image->upload($this->request->param(), $file, $this->request->user, 1);
                /* 记录附件信息 */
                if (0 == $data['code']) {
                    $return['url'] = $data['data']['url'];
                    $return['error'] = 0;
                } else {
                    $return['error'] = 1;
                    $return['message'] = $data['message'];
                }
            } else {
                $return['error'] = 1;
                $return['message'] = '获取上传信息错误';
            }
            /* 返回JSON数据 */
            return json($return);
        }

        public function config()
        {
            if ($this->request->file('imgFile')) {
                $file = $this->request->file('imgFile');
                $image = new ImageEvent();
                $data = $image->upload($this->request->param(), $file, $this->request->user, 1);
                /* 记录附件信息 */
                if (0 == $data['code']) {
                    $return['url'] = $data['data']['url'];
                    $return['error'] = 0;
                } else {
                    $return['error'] = 1;
                    $return['message'] = $data['message'];
                }
            } else {
                $return['error'] = 1;
                $return['message'] = '获取上传信息错误';
            }
            /* 返回JSON数据 */
            return json($return);
        }

        /**
         * 获取根目录.
         */
        public function getDirRoot()
        {
            // 添加公告文件夹
            $common['datetime'] = time_format();
            $common['filename'] = 'common';
            $common['has_file'] = true;
            $common['is_dir'] = true;
            $dir[] = $common;
            $save_path = Db::name('image')->distinct(true)
                ->where('save_path', '<>', '')
                ->column('save_path');
            if ($save_path) {
                foreach ($save_path as $value) {
                    $common['datetime'] = time_format();
                    $common['filename'] = str_replace('.\\uploads\\image\\', '', $value);
                    $common['has_file'] = true;
                    $common['is_dir'] = true;
                    $dir[] = $common;
                }
            }
            // $data['current_dir_path']='/uploads/image/';
            // $data['current_url']='/uploads/image/';
            $data['current_dir_path'] = '';
            $data['current_url'] = '';
            $data['file_list'] = $dir;
            $data['moveup_dir_path'] = '';
            $data['total_count'] = count($dir);
            /* 返回JSON数据 */
            return json($data);
        }

        public function manager()
        {
            return json(['']);
            $dir = $this->request->get('path');
            if (!$dir) {
                return $this->getDirRoot();
            } else {
                // 获取文件列表
                // dump(str_replace('/','',$dir));
                $images = Db::name('image')->where('save_path', 'like', '%' . str_replace('/', '', $dir) . '%')->select();
                $lists = [];
                if ($images) {
                    foreach ($images as $image) {
                        $file['datetime'] = time_format($image['create_time']);
                        $file['dir_path'] = '/uploads/image/';
                        // $file['filename']=$image['save_name'];
                        // $file['dir_path']='';
                        $file['filename'] = $image['url'] ?: (str_replace('.', '', $image['save_path']) . DS . $image['save_name']);
                        $file['filesize'] = $image['size'];
                        $file['filetype'] = $image['ext'];
                        $file['has_file'] = false;
                        $file['is_dir'] = false;
                        $file['is_photo'] = true;
                        $lists[] = $file;
                    }
                }
                $data['current_dir_path'] = $dir;
                $data['current_url'] = '';
                // $data['current_url']='/uploads/image/'.$dir;
                // $data['current_dir_path']=$dir;
                // $data['current_url']=$dir;
                $data['file_list'] = $lists;
                $data['moveup_dir_path'] = '';
                $data['total_count'] = count($lists);
                /* 返回JSON数据 */
                return Response::create($data, 'json');
            }
        }
    }
